package com.czmh.etc.onlinepubsys.controller.mingchuang;

import com.alibaba.fastjson.JSON;
import com.czmh.etc.onlinepubsys.constants.mingchuang.BusinessType;
import com.czmh.etc.onlinepubsys.entity.mingchuang.ApplyInfo;
import com.czmh.etc.onlinepubsys.entity.mingchuang.CarInfo;
import com.czmh.etc.onlinepubsys.entity.mingchuang.CurrentContext;
import com.czmh.etc.onlinepubsys.feign.newrun.constans.UserProperty;
import com.czmh.etc.onlinepubsys.json.mingchuang.JSONResult;
import com.czmh.etc.onlinepubsys.request.mingchuang.ApplyExtInfo;
import com.czmh.etc.onlinepubsys.request.mingchuang.CarInfoRequest;
import com.czmh.etc.onlinepubsys.request.mingchuang.CheckCarNoRequest;
import com.czmh.etc.onlinepubsys.services.mingchuang.ICarInfoService;
import com.czmh.etc.onlinepubsys.services.mingchuang.ICurrentContextService;
import com.czmh.etc.onlinepubsys.services.mingchuang.IEtcApplyService;
import com.czmh.etc.onlinepubsys.utils.mingchuang.HttpUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/{version}/api/mingchang/carInfo")
@Api(tags = "车辆信息接口【新】- 铭创")
public class CarInfoController {
    @Autowired
    private UserProperty userProperty;

    @Autowired
    private ICarInfoService carInfoService;
    @Autowired
    private IEtcApplyService etcApplyService;
    @Autowired
    private ICurrentContextService currentContextService;

    @PostMapping("/addCarInfo")
    @ApiOperation("添加车辆信息 1198")
    public JSONResult addCarInfo(@RequestBody CarInfoRequest req) {
        log.info("添加车辆信息:{}", JSON.toJSONString(req));
        CarInfo vehicle = req.getVehicle();
        vehicle.setWheelCount(4);
        vehicle.setAxleCount(2);
        String userId = vehicle.getUserId();

        CurrentContext ctx = currentContextService.getByUserId(userId);

        ApplyInfo applyInfo = etcApplyService.get(ctx.getApplyId());
        req.setClientNo(userProperty.getClientNo());
        req.setFundAccountId(ctx.getFundAccountId());
        JSONResult result = HttpUtil.post(BusinessType.ADD_CAR_MSG, req);

        if (result.getCode().equals(JSONResult.SUCCESS)) {
            String vehPlate = vehicle.getVehPlate();
            log.info("保存车辆信息,用户ID:{},车牌号:{}", userId, vehPlate);
            carInfoService.save(vehicle);
            //处理用户上传的图片信息
            if(StringUtils.isNotEmpty(applyInfo.getJsonData())){
                ApplyExtInfo applyExtInfo = JSON.parseObject(applyInfo.getJsonData(), ApplyExtInfo.class);
                applyExtInfo.setVehCertBackImgUrl(req.getApplyExtInfo().getVehCertBackImgUrl());
                applyExtInfo.setVehCertFrontImgUrl(req.getApplyExtInfo().getVehCertFrontImgUrl());
                applyInfo.setJsonData(JSON.toJSONString(applyExtInfo));
            }
            applyInfo.setVehCertFrontImg(req.getVehCertFrontImg());
            applyInfo.setVehCertbackImg(req.getVehCertBackImg());
            applyInfo.setVehiclePlate(vehPlate);
            applyInfo.setVehicleColor(vehicle.getVehColor());
            applyInfo.setVehicleType(req.getVehicle().getType());

            etcApplyService.update(applyInfo);

            //更新进行时
            ctx.setCarId(vehicle.getId());
            ctx.setState(3); //已添加车辆信息
            currentContextService.update(ctx);
        }
        return result;
    }

    @PostMapping("/checkCarNo")
    @ApiOperation("检查车牌号唯一性 6001")
    public JSONResult check(@RequestBody CheckCarNoRequest req) {
        log.info("检查车牌唯一性");
        req.setClientNo(userProperty.getClientNo());
        return HttpUtil.post(BusinessType.CHECK_CAR_NO, req);
    }
}
